package com.woniu.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.domain.Classify;
import com.woniu.mapper.ClassifyMapper;
import com.woniu.service.ClassifyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.RedisTemplate;
import com.woniu.vo.ClassifyVo;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyy
 * @since 2021-03-05
 */
@Service
public class ClassifyServiceImpl extends ServiceImpl<ClassifyMapper, Classify> implements ClassifyService {
    @Resource
    private RedisTemplate<String, Classify> redisTemplate;

    @Resource
    private ClassifyMapper classifyMapper;

    //往redis里面存储一级二级
    @Override
    public List<Classify> getOneTwoClassify() {
        //找到所有的一级二级分类
        ListOperations<String, Classify> listOperations = redisTemplate.opsForList();
        List<Classify> classifyOne = listOperations.range("classifyOnes", 0, -1);

        if (ObjectUtils.isEmpty(classifyOne)) {
         synchronized (this){
             classifyOne = listOperations.range("classifyOnes", 0, -1);
             if(ObjectUtils.isEmpty(classifyOne)){
                 //从数据库中获取数据
                 List <Classify> classifies=classifyMapper.selectList(null);
                 LinkedHashSet<Classify> needClassifies=new LinkedHashSet<>();
                 classifies.forEach(classify -> {
                     if(classify.getLevel()==1){
                         needClassifies.add(classify);
                         classify.setChildren(new LinkedHashSet<Classify>());
                     }
                 });
                 classifies.forEach(classify -> {
                     needClassifies.forEach(first->{
                         if(classify.getCid()==first.getId()){
                             first.getChildren().add(classify);
                         }
                     });
                 });
                 //需要的lists
                 ArrayList<Classify> lists = new ArrayList(needClassifies);
                 //存储到Redis中
                 LinkedHashSet<Classify> redisNeedClassifies = new LinkedHashSet<>();  //一级目录下的子目录存储
                 for (int i=1;i<=lists.size();i++){
                     //给一级目录下面的所有需要的子目录赋一个存储空间;
                     lists.get(i - 1).setChildren2(new LinkedHashSet<ClassifyVo>());
                     //得到的是一个父层级下面的所有子目录
                     ArrayList<Classify> childrens = new ArrayList<Classify>(lists.get(i-1).getChildren());
                     //一一赋值
                     ArrayList<ClassifyVo> children2 = new ArrayList<ClassifyVo>(lists.get(i - 1).getChildren2());
                     for(int j=1;j<=childrens.size();j++){
                         ClassifyVo classifyVo = new ClassifyVo();
                         boolean add = children2.add(classifyVo);
                         if(add){
                             children2.get(j-1).setId(childrens.get(j-1).getCid()+"-"+j);
                             children2.get(j-1).setName(childrens.get(j-1).getName());
                         }
                     }
                     System.out.println(children2);
                     redisNeedClassifies.add(lists.get(i-1).setChildren2(new LinkedHashSet<ClassifyVo>(children2)).setChildren(null));
                 }
                 classifyOne = new ArrayList<>(redisNeedClassifies);

                 for (int i = 0; i < redisNeedClassifies.size(); i++) {
                     listOperations.rightPush("classifyOnes",classifyOne.get(i));
                 }
                 System.out.println("数据库查出------------"+classifyOne+"\n");
                 return classifyOne;
             }
         }
        }
       ArrayList<Classify> classifyneed=new ArrayList<Classify>();
        for(Object classify:classifyOne){
            String s = JSON.toJSONString(classify);
            Classify reverseClassify= JSONObject.parseObject(s,Classify.class);
            classifyneed.add(reverseClassify);
        }
        System.out.println("redis查出------------"+classifyneed+"\n");
        return classifyneed;
    }


    @Override
    public List<Classify> smell(Integer cid) {
        List<Classify> list=redisTemplate.opsForList().range("level:smell"+cid,0,-1);
        List<Classify> list1;
        if (ObjectUtils.isEmpty(list)){
            QueryWrapper<Classify> wrapper = new QueryWrapper<>();
            wrapper.eq("cid",cid);
            wrapper.eq("level",2);
            list1=this.list(wrapper);
            redisTemplate.opsForList().leftPushAll("level:smell"+cid,list1);
        }else {
            list1=list;
        }
        return list1;
    }
}
